<template>
	<div>
    <div style="margin-bottom: 20px">
      <el-button type="primary" @click="templateAdd()"><i class="el-icon-plus"></i>开机广告</el-button>
      <!-- <el-button type="primary" @click="setSortDialog = true"><i class="el-icon-plus"></i>排序</el-button> -->
    </div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" label-width="70px">
      <el-form-item label="分公司" label-width="60px">
        <el-select v-if="!brachDisabled" v-model="formInline.branchId" :disabled="brachDisabled" style="width:160px">
          <el-option v-for="(item, index) in this.branchList" :value="item.branch_Id" :label="item.branch_Name" :key="index"></el-option>
        </el-select>
        <el-input v-if="brachDisabled" v-model="showBranch" :disabled="brachDisabled" style="width:160px"></el-input>
      </el-form-item>
      <el-form-item label="所属设备">
        <el-select v-model="formInline.equipment" placeholder="所属系统" clearable style="width:110px">
          <el-option v-for="(item, index) in equipmentGroup" :value="item.id" :label="item.name" :key="index" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" label-width="40px">
        <el-input v-model="formInline.name" placeholder="请输入名称" style="width:100px"></el-input>
      </el-form-item>
      <el-form-item label="ID" label-width="30px">
        <el-input v-model="formInline.id" placeholder="请输入ID" style="width:80px"></el-input>
      </el-form-item>
      <el-form-item label="时间" label-width="40px">
        <el-date-picker v-model="startStopTime" type="daterange" value-format="yyyy-MM-dd" @change="daterangeFormat" placeholder="请选择起止时间" style="width:215px"></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" label-width="40px">
        <el-select v-model="formInline.cstate" placeholder="状态" @change="pageChange" clearable style="width:80px">
          <el-option label="草稿" value="1"></el-option>
          <el-option label="待审核" value="2"></el-option>
          <el-option label="未开始" value="3"></el-option>
          <el-option label="进行中" value="4"></el-option>
          <el-option label="已结束" value="5"></el-option>
          <el-option label="下线待审核" value="6"></el-option>
          <el-option label="已下线" value="7"></el-option>
          <el-option label="审核不通过" value="8"></el-option>
          <!-- <el-option label="审核通过" value="9"></el-option> -->
        </el-select>
      </el-form-item>
			<el-form-item  style="float:right">
        <el-button type="primary" @click="searchList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="isLoading" :data="tableData" style="width: 100%" border>
      <el-table-column prop="cid" label="ID" align="center" width="100"></el-table-column>
      <!-- <el-table-column label="排序" align="center"></el-table-column> -->
      <el-table-column prop="branchName" label="分公司" align="center"></el-table-column>
      <el-table-column prop="usertypeName" label="客资" align="center"></el-table-column>
      <el-table-column prop="equipmentName" label="设备类型" align="center"></el-table-column>
      <el-table-column prop="adTypeName" label="广告类型" align="center" width="100"></el-table-column>
      <el-table-column prop="cadname" label="广告名称" align="center" width="100"></el-table-column>
      <el-table-column prop="actionname" label="跳转类型" align="center"></el-table-column>
      <el-table-column prop="orderuptime" label="开始时间" align="center"></el-table-column>
      <el-table-column prop="offlinetime" label="结束时间" align="center"></el-table-column>
      <el-table-column prop="createtime" label="创建时间" align="center"></el-table-column>
      <el-table-column prop="cstate" label="状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template scope="scope">
          <el-button v-if="scope.row.cstate === '草稿'" @click="handle(scope.$index, scope.row, $event)" size="small">编辑</el-button>
          <el-button v-if="scope.row.cstate === '待审核' || scope.row.cstate === '未开始' || scope.row.cstate === '进行中' || scope.row.cstate === '已下线' || scope.row.cstate === '审核不通过'
          || scope.row.cstate === '已结束' || scope.row.cstate === '下线待审核'" @click="handle(scope.$index, scope.row, $event)" size="small">详情</el-button>
          <el-button v-if="scope.row.cstate === '草稿'" @click="handle(scope.$index, scope.row, $event)" size="small">删除</el-button>
          <el-button v-if="scope.row.cstate === '待审核'" @click="handle(scope.$index, scope.row, $event)" size="small">撤回</el-button>
          <el-button v-if="scope.row.cstate === '未开始' || scope.row.cstate === '进行中'" @click="handle(scope.$index, scope.row, $event)" size="small">下线</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="Number(formInline.pageSize)"
      layout="total, prev, pager, next"
      :total="Number(formInline.total)"
      style="padding-top:10px;text-align: right;">
    </el-pagination>
    <el-dialog title="跳转动作详情" :visible.sync="coupontDeteil" v-if="coupontDeteil">
      <el-form label-width="180px">
        <div class="detail-info" style="margin-top: 43px">
          <div class="form-item">
            <h4>基本信息</h4>
            <el-form-item label="分公司">
              <span>{{this.detailData.branchName}}</span>
            </el-form-item>
            <el-form-item label="客资类型">
              <span>{{this.detailData.usertypeName}}</span>
            </el-form-item>
            <el-form-item label="广告类型">
              <span>{{this.detailData.adTypeName}}</span>
            </el-form-item>
            <el-form-item label="所属设备">
              <span>{{this.detailData.equipmentName}}</span>
            </el-form-item>
            <el-form-item label="起止时间">
              <span>{{this.detailData.orderuptime}} - {{this.detailData.offlinetime}}</span>
            </el-form-item>
            <el-form-item label="广告名称">
              <span>{{this.detailData.cadname}}</span>
            </el-form-item>
            <el-form-item label="触发次数">
              <span v-if="this.detailData.touchType === 1">全程</span>
              <span v-if="this.detailData.touchType === 2">每天</span>
              <span v-if="this.detailData.touchType === 3">不限制</span>
            </el-form-item>
            <el-form-item label="次数" v-if="this.detailData.touchType !== 3">
              <span>{{this.detailData.times}}</span>
            </el-form-item>
          </div>
        </div>
        <div class="detail-info" style="margin-top: 43px" v-if="this.detailData.detail[0].adimage !== ''">
          <div class="form-item">
            <h4>PC订货</h4>
            <el-form-item label="图片">
              <img :src="this.imgHost + '/' + this.detailData.detail[0].adimage" class="avatar">
            </el-form-item>
            <el-form-item label="跳转方式">
              <span>{{this.detailData.detail[0].actionname}}</span>
            </el-form-item>
            <el-form-item>
              <span>{{this.detailData.detail[0].adurl}}</span>
            </el-form-item>
          </div>
        </div>
        <div class="detail-info" style="margin-top: 43px;margin-bottom:80px">
          <div class="form-item">
            <h4>审核信息</h4>
            <el-table :data="this.detailData.checkInfo" border>
              <el-table-column prop="auditor" label="审核人名称" align="center"></el-table-column>
              <!-- <el-table-column prop="content" label="任务内容" align="center"></el-table-column> -->
              <el-table-column prop="resultName" label="审核结果" align="center"></el-table-column>
              <el-table-column prop="opinion" label="审核意见" align="center"></el-table-column>
              <el-table-column prop="createTime" label="审核时间" align="center"></el-table-column>
            </el-table>
          </div>
        </div>
      </el-form>
    </el-dialog>
    <el-dialog title="排序" :visible.sync="setSortDialog">
      <el-form :inline="true" style="margin-top:20px">
        <el-form-item label="分公司" label-width="60px">
          <el-select v-if="!brachDisabled" v-model="sortInfo.branchId" :disabled="brachDisabled" style="width:150px">
            <el-option v-for="(item, index) in this.branchList" :value="item.branch_Id" :label="item.branch_Name" :key="index"></el-option>
          </el-select>
          <el-input v-if="brachDisabled" v-model="showBranch" :disabled="brachDisabled" style="width:150px"></el-input>
        </el-form-item>
        <el-form-item label="客资类型">
          <el-select v-model="sortInfo.usertype" placeholder="客资类型" style="width:150px" :loading="customerTypeLoading">
            <!-- <el-option value="1" label="普通超市"></el-option> -->
            <el-option v-for="(item, index) in customerTypeList"
            :key="index"
            :label="item.name"
            :value="item.id.toString()"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属设备">
          <el-select v-model="sortInfo.equipment" placeholder="所属设备" style="width:150px">
            <el-option label="PC订货" value="1"></el-option>
            <el-option label="惠付通" value="2"></el-option>
            <el-option label="惠配通" value="3"></el-option>
            <el-option label="惠付通-款台" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-button type="primary" style="float:right" @click="searchList2">查询</el-button>
      </el-form>
      <el-table :data="setSortTableList" border v-loading="isLoading2">
        <el-table-column label="序号" prop="bannerSort" align="center"></el-table-column>
        <el-table-column label="广告ID" prop="cid" align="center"></el-table-column>
        <el-table-column label="广告名称" prop="cadname" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template scope="scope" align="center">
            <el-button size="small" v-if="scope.$index !== 0"
            @click="handleSetSort('置顶', scope.$index)">置顶</el-button>
            <el-button size="small" v-if="scope.$index !== 0"
            @click="handleSetSort('上移', scope.$index)">上移</el-button>
            <el-button size="small" v-if="scope.$index !== setSortTableList.length - 1"
            @click="handleSetSort('下移', scope.$index)">下移</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setSortDialog = false">取消</el-button>
        <el-button type="primary" @click="sortEdit">确 定</el-button>
      </div>
    </el-dialog>
	</div>
</template>
<script>
  import { gggl, getInfo } from '../../../../../config/proxy.js'
  import mixins from '../../../../../components/mixins.vue'
  import { apiHost } from '../../../../../config/settings.js'
  export default {
    mixins: [mixins],
    data () {
      return {
        formInline: {
          branchId: '', // 分公司id
          equipment: '', // 设备类型
          name: '', // 名称
          id: '', // id
          cstate: '', // 状态
          orderuptime: '', // 开始时间
          offlinetime: '', // 结束时间
          pageSize: 10,
          // sortType: '1', // 排序类型
          page: 1,
          total: ''
        },
        startStopTime: '',
        equipmentGroup: [
          {name: 'PC订货', id: 1},
          {name: '惠付通', id: 2},
          {name: '惠配通', id: 3}
        ], // 广告数组
        showBranch: '', // 展示分公司
        brachDisabled: false,
        coupontDeteil: false,
        branchList: [],
        couponInfoList: [],
        checkInfoList: [],
        departmentList: [],
        tableData: [],
        isLoading: false,
        isLoading2: false,
        detailData: '',
        detailDialog: false,
        sortInfo: {
          usertype: '', // 客资类型
          branchId: '',
          equipment: '',
          sortType: '2',
          page: 1,
          pageSize: 1000
        },
        setSortDialog: false, // 排序弹框
        customerTypeLoading: false, // 客资类型加载
        customerTypeList: [], // 客资类型数组
        setSortTableList: [],
        imgHost: ''
      }
    },
    methods: {
      pageChange () {
        this.formInline.page = 1
      },
      templateAdd () { // 新增款台广告
        this.openNewWindow('kjgggl_add', '新增开机广告', 'kjgggl_add')
      },
      searchAd () { // 查询广告
        var _data = {
          system: '2',
          page: 1,
          pageSize: 10000
        }
        gggl.mkgl.list(_data, (res) => { // 查询
          console.log(res)
          if (res.message === 'ok') {
            this.markidGroup = res.content.content
          } else {
            this.message('查询失败', 'error')
          }
        })
      },
      searchCustomerType () { // 查询客资类型
        var _data = ''
        getInfo.client(_data, (res) => {
          console.log(res)
          if (res.message === 'ok') {
            this.customerTypeList = res.content.content
            this.customerTypeLoading = false
          }
        })
      },
      searchList () { // 查询
        // this.tableData = []
        this.isLoading = true
        var _data = this.formInline
        console.log(_data)
        gggl.dhxtgg.kjgg.list(_data, (res) => { // 查询
          console.log(res)
          if (res.message === 'ok') {
            this.isLoading = false
            this.tableData = res.content.content
            this.formInline.total = res.content.count
            this.formInline.page = res.content.page
            this.matchState(res.content.content)
          } else {
            this.isLoading = false
            this.message('查询失败', 'error')
          }
        })
      },
      searchList2 () {
        this.isLoading2 = true
        var _data = this.sortInfo
        console.log(_data)
        gggl.dhxtgg.kjgg.list(_data, (res) => { // 查询
          console.log(res)
          if (res.message === 'ok') {
            this.isLoading2 = false
            this.setSortTableList = res.content.content
            // this.formInline.total = res.content.count
            // this.formInline.page = res.content.page
            // this.matchState(res.content.content)
          } else {
            this.message('查询失败', 'error')
          }
        })
      },
      // clickSortDetail () { // 排序点击按钮
      //   this.setSortDialog = true
      // },
      handleSetSort (text, index) { // 排序
        // this.setSortDialog = true
        // console.log()
        switch (text) {
          case '置顶':
            this.setSortTableList[index] = this.setSortTableList.splice(0, 1, this.setSortTableList[index])[0]
            console.log(this.setSortTableList[index])
            break
          case '上移':
            this.setSortTableList[index] = this.setSortTableList.splice(index - 1, 1, this.setSortTableList[index])[0]
            console.log(this.setSortTableList[index])
            break
          default:
            this.setSortTableList[index] = this.setSortTableList.splice(index + 1, 1, this.setSortTableList[index])[0]
            console.log(this.setSortTableList[index])
            break
        }
        for (let index = 0; index < this.setSortTableList.length; index++) {
          this.setSortTableList[index].bannerSort = (index + 1) * 10
        }
      },
      sortEdit () { // 提交排序
        console.log(this.setSortTableList)
        var _data = []
        for (let index = 0; index < this.setSortTableList.length; index++) {
          const element = this.setSortTableList[index]
          var a = {}
          a.cid = element.cid
          a.bannerSort = element.bannerSort
          _data.push(a)
        }
        console.log(_data)
        gggl.dhxtgg.kjgg.sortEdit(_data, res => {
          // console.log(res)
          if (res.message === 'ok') {
            this.searchList2()
            this.setSortDialog = false
          } else {
            this.message('排序失败', 'error')
          }
        })
      },
      matchState (res) { // 匹配状态
        if (res) {
          for (var i = 0; i < res.length; i++) {
            if (res[i].cstate === 1) { res[i].cstate = '草稿' }
            if (res[i].cstate === 2) { res[i].cstate = '待审核' }
            if (res[i].cstate === 3) { res[i].cstate = '未开始' }
            if (res[i].cstate === 4) { res[i].cstate = '进行中' }
            if (res[i].cstate === 5) { res[i].cstate = '已结束' }
            if (res[i].cstate === 6) { res[i].cstate = '下线待审核' }
            if (res[i].cstate === 7) { res[i].cstate = '已下线' }
            if (res[i].cstate === 8) { res[i].cstate = '审核不通过' }
            if (res[i].cstate === 9) { res[i].cstate = '审核通过' }
          }
        }
      },
      handleCurrentChange (val) { // 分页
        this.formInline.page = val
        this.tableData = []
        this.isLoading = true
        this.searchList()
      },
      handle (index, row, event) { // 操作
        const handleName = event.target.innerText
        switch (handleName) {
          case '详情':
            console.log(row.cid)
            this.imgHost = apiHost.imgHost
            gggl.dhxtgg.kjgg.detail({id: row.cid}, (res) => {
              console.log(res, '详情')
              if (res.message === 'ok') {
                this.detailData = res.content
                this.coupontDeteil = true
              } else {
                this.message('查看详情', 'error')
              }
            })
            // this.openNewWindow('zcfqDetail', '注册返券详情', 'zcfqDetail', row.id)
            break
          case '编辑':
            this.openNewWindow('kjgggl_bj', '编辑开机广告', 'kjgggl_add', row)
            break
          case '复制':
            this.openNewWindow('kjgggl_bj', '复制开机广告', 'kjgggl_add')
            break
          case '删除':
            this.buttonPublicMethods('删除', 'delete', row)
            break
          case '撤回':
            this.buttonPublicMethods('撤回', 'revoke', row)
            break
          // case '作废':
          //   this.buttonPublicMethods('作废', 'delete', row)
          //   break
          case '下线':
            this.buttonPublicMethods('下线', 'offLine', row)
            break
        }
      },
      buttonPublicMethods (text, fnNane, row) { // 删除 撤回 作废 公共方法
        this.$confirm('您确定要' + text + '吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var _data = {}
          _data.cid = row.cid
          console.log(_data)
          console.log(fnNane)
          gggl.dhxtgg.kjgg[fnNane](_data, (res) => {
            console.log(res)
            if (res.message === 'ok') {
              this.searchList()
              this.message(text + '成功', 'success')
              this.$store.dispatch('kjggState', this.$store.state.updateState.kjggState + 1)
            } else {
              this.message(text + '失败', 'error')
            }
          })
        }).catch(() => {
        })
      },
      // openNewWindow (menuId, menuName, perController, uid) { // 新增tab方法
      //   var _data = {
      //     menu_id: menuId,
      //     menu_name: menuName,
      //     per_controller: perController
      //   }
      //   if (uid) {
      //     _data.uid = uid
      //   }
      //   this.$store.dispatch('addTab', _data)
      // },
      timeDeltSecond (d) { // 时间去秒
        if (d) {
          return d.split(' ')[0]
        }
      },
      daterangeFormat (t) { // 时间段格式化
        this.formInline.orderuptime = t[0]
        this.formInline.offlinetime = t[1]
      },
      message (text, type) { // 消息提示
        this.$message({
          showClose: true,
          message: text,
          type: type
        })
      }
    },
    computed: {
      update_state: function () {
        // console.log(state.updateState.dhmState)
        return this.$store.state.updateState.kjggState
      },
      userName: function () {
        if (!this.$store.state.user.info.user_name) {
          this.$store.dispatch('SET_TOKEN')
        }
        return this.$store.state.user.info.user_name
      },
      userId: function () {
        return this.$store.state.user.info.roleid
      },
      branchName: function () {
        return this.$store.state.user.info.branchId === 1 ? '总公司' : this.$store.state.user.info.branchName
      },
      branchId: function () {
        return this.$store.state.user.info.branchId
      },
      deptName: function () {
        return this.$store.state.user.info.parOrgName
      },
      deptId: function () {
        return this.$store.state.user.info.or_org_id
      }
    },
    watch: {
      update_state (curVal, oldVal) {  // 监听 更新状态 查询list
        this.searchList()
      }
    },
    created () {
      // this.searchAd() // 查询广告
      this.searchCustomerType()
      if (this.branchId === 1) {
        this.brachDisabled = false
        this.formInline.branchId = this.branchId
        this.sortInfo.branchId = this.branchId
      } else {
        this.brachDisabled = true
        this.showBranch = this.branchName
        this.sortInfo.branchId = this.branchId
        this.formInline.branchId = this.branchId
      }
    }
  }
</script>
<style>
.line{
	margin-left:20px;
	text-align:center
}
.el-button+.el-button{
  margin-left: 0
}
.el-table .cell, .el-table th>div{
  padding-left:10px;
  padding-right:10px;
}
.el-dialog--small{
  width:60%
}
</style>
